<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcampaignuser extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
   
	public function getCampaignUser($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql = "SELECT c.*, u.fullname AS agency_fullname FROM campaign AS c LEFT JOIN user AS u ON u.id = c.id_agency WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function getCampaignReport()
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC LIMIT 0,3";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
             $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
            $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
            $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
            $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
        }
/////////////////////////////////////////////////
//////////phân loại nhóm khách hàng /////////////
/////////////////////////////////////////////////
         $sql_cc = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        $sql_cc .= " ORDER BY c.id DESC";
        $xs_cc = $this->db->query($sql_cc)->result_object();
        $arr_cc =array();
        foreach ( $xs_cc AS $el=>$le ){
            $data_cc1 = $this->db->query('SELECT * FROM classify_device WHERE `group` = 1')->result_object();
            $count_gr = array();
            foreach ( $data_cc1 AS $ml1=>$lm1 ){ 
                $dem_cc1 = 0;
                $dem_cc1 += $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => (string)$le->id_shop, 'os_name' => $lm1->os_name ))->count('log_access');
            }
             $arr_cc1[] = $dem_cc1;
             
              $data_cc2 = $this->db->query('SELECT * FROM classify_device WHERE `group` = 2')->result_object();
            $count_gr = array();
            foreach ( $data_cc2 AS $ml2=>$lm2 ){ 
                $dem_cc2 = 0;
                $dem_cc2 += $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => (string)$le->id_shop, 'os_name' => $lm2->os_name ))->count('log_access');
            }
             $arr_cc2[] = $dem_cc2;
             
              $data_cc3 = $this->db->query('SELECT * FROM classify_device WHERE `group` = 3')->result_object();
            $count_gr = array();
            foreach ( $data_cc3 AS $ml3=>$lm3 ){ 
                $dem_cc3 = 0;
                $dem_cc3 += $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => (string)$le->id_shop, 'os_name' => $lm3->os_name ))->count('log_access');
            }
             $arr_cc3[] = $dem_cc3;
             
             
        } // END foreach
        $meme1 = $meme2 = $meme3 = 0;    
        foreach ( $arr_cc1 AS $item1 ){
            $meme1+= $item1;
        }
        foreach ( $arr_cc2 AS $item2 ){
            $meme2+= $item2;
        }
        foreach ( $arr_cc3 AS $item3 ){
            $meme3+= $item3;
        }
        $total_cc = $meme1 + $meme2 + $meme3;
        $arr_[0] = number_format(($meme1/$total_cc)*100, 2, '.', '');
        $arr_[1] = number_format(($meme2/$total_cc)*100, 2, '.', '');
        $arr_[2] = 100 - number_format(($meme1/$total_cc)*100, 2, '.', '') - number_format(($meme2/$total_cc)*100, 2, '.', '');
        return array('data' => $xs, 'arr_' => $arr_);
    }
    
    
      public function ajaxGetCampaignReport($last_id)
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC LIMIT $last_id,3";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
            
             $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
             $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
            $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
            $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
        }
        return array('data' => $xs);
    }
    
    public function getPlace()
    {
        $place_data = $this->db->query('SELECT id, shopName AS placeName, address, lat, `long`, logo FROM shop')->result_object();
        
        $arr = $place_data;
        foreach($place_data AS $el=>$le)
        {
            $arr[$el]->campaign = $this->db->query('SELECT c.id AS c_id, cp.id_place AS p_id, c.title FROM campaign AS c INNER JOIN campaign_place AS cp ON cp.id_campaign = c.id WHERE c.active = 2 AND cp.id_place = '.$le->id.' AND c.id_customers = '.$this->session->userdata('id_user'))->result_object();
        }
        return $arr;
    }
	
	public function getListShop($id)
    {
	//die('1');
        $data = $this->db->query('SELECT id, shopName, address FROM shop ORDER BY id ASC')->result_object();
        $html = '<option value = "0">Chọn cửa hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->shopName.'-'.$le->address.'</option>';
        }
        return $html;
    }
	
	public function dataListShop(){
		return $this->db->query('SELECT * FROM shop ORDER BY id ASC ')->result_object();
	}
	
	public function addCampaignUser($type_ads,$address_id,$title,$time_start,$time_end,$note,$cost){
            $id_agency = $this->db->query('SELECT agency FROM user WHERE id = '.$this->session->userdata('id_user'))->row_object()->agency;
            $data = array(
            	'id_customers' => $this->session->userdata('id_user'),
            	'title' => $title,
            	'time_start' => $time_start,
            	'time_end' => $time_end,
            	'note' => $note,
            	'folder_campaign' => 'html2',
            	'active' => (int)0,
            	'created' => date('Y-m-d H:i:s'),
            	'id_agency' => $id_agency,
                'type_ads' => (int)$type_ads,
                'cost' => (int)$cost,
                'cost_is_used' => (int)0
            );
            	
            $this->db->insert('campaign', $data);
            $id_campaign = $this->db->insert_id();
            // trừ tiền vào tài khoản
            $moneydb = $this->db->query('SELECT money FROM user WHERE id = '.$this->session->userdata('id_user'))->row_object()->money;
            $moneydb_new = (int)$moneydb - (int)$cost;
            $this->db->update('user', array('money' => $moneydb_new ), "id = ".$this->session->userdata('id_user'));
            $this->session->set_userdata(array('money' => $moneydb_new));          
            
            
            // thêm vào lịch sử giao dịch
            $data_trade = array ('id_campaign' => $id_campaign, 'money' => $cost , 'created' => date('Y-m-d H:i:s'));
            $this->db->insert('log_trade', $data_trade );
            
            foreach ( $address_id AS $item ){
                $xs = $this->db->query('SELECT id, shopName, address, cost FROM shop WHERE id = '.$item.' ORDER BY id ASC')->row_object();
                $data_cost = $xs->cost;
                foreach ( json_decode($data_cost) AS $el=>$le ){
                    if( $el == $type_ads ){
                       //die('-'.print_r(array ( 'id_campaign' => $id_campaign, 'id_place' => $item, 'cost' => $le->cost   )));
                        $this->db->insert('campaign_place', array ( 'id_campaign' => $id_campaign, 'id_place' => $item, 'cost' => $le->cost   ));
                    }
                } 
            }
		  return 200;
	}
	
	public function deleteCampaignUserDb($id){
        //$this->db->delete('campaign', array('id' => $id)); 
		$this->db->update('campaign', array('active' => (int)4 ), "id = ".$id);
        return 200;
	}

	

	
	public function getCampaignuserDetail($id){
		return array('data' => $this->db->query('SELECT c.*, u.fullname FROM campaign AS c JOIN user AS u ON c.id_customers = u.id  WHERE c.id = '.$id)->row_object(), 'place' => $this->db->query('SELECT * FROM campaign_place WHERE id_campaign = '.$id)->result_object() ) ;
	}
    
    
    /**
     *      
     * @void Thống kê function
     */
     
    public function getNamePlace($p_id, $c_id)
    {
       return $this->db->query('SELECT campaign.title, shop.id, shop.shopName FROM campaign INNER JOIN campaign_place ON campaign.id = campaign_place.id_campaign INNER JOIN shop ON campaign_place.id_place = shop.id WHERE campaign.id = '.$c_id.' AND shop.id = '.$p_id)->row_object();
    }
    
    public function getTimeCampaign($id_campaign,$id_user)
    {
        
        $mq = $this->db->query('SELECT id,title,code_campaign, DATE_FORMAT(time_start,"%d/%m/%Y %H:%i") AS time1,DATE_FORMAT(time_end,"%d/%m/%Y %H:%i") AS time2, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time_1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time_2 FROM campaign WHERE id ='.$id_campaign)->row_object();
        $time_select = $mq->time1.' - '.$mq->time2;
        $time        = $mq->time_1.'|'.$mq->time_2;
        return (object)array('time_select' => $time_select, 'time' => $time, 'title' => '['.$mq->code_campaign.'] - ' . $mq->title);
    }
    
    public function getChartAb($id = 2, $time, $type, $id_user)
    {
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id);
        
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            $data = $this->m_db->where(array('id_campaign' => intval($id)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
            $dem = 0;
            foreach($data AS $le=>$el)
            {
                $dem =  $dem + 1;
                if($el['show_time'] == 'Other')
                {
                    $key = 'Other';
                }else if(in_array($el['show_time'], array(null, '', '0')))
                {
                    $key = 'Other';
                }else{
                    $key = '';
                    $key = $el['show_time'];
                }
                @$array[$key] = $array[$key] + 1;
            }
            $array['tong'] = $dem;
            //ksort($array);
            return $array;
        }
        return 201;
        
    }
    
    public function getCount($id_user,$time,$id_campaign)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $array_os = array();
        $dem_os = 0;
        $key_os = '';
        
        $screen = 0;
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
            
       
            foreach($data AS $le=>$el)
            {
                
                if($el['browser_name_group'] == 'Other')
                {
                    $dem =  $dem + 1;
                    $key = 'Chưa xác định';
                }else if(in_array($el['browser_name_group'], array(null, '', '0')))
                {
                    $dem =  $dem + 1;
                    $key = 'Chưa xác định';
                }else{
                    $dem =  $dem + 1;
                    $key = '';
                    $key = $el['browser_name_group'];
                }
                @$array[$key] = $array[$key] + 1;
                
                
                /**
                 * 
                 * @void đếm màn hình
                 */
                $key = '';
                if(@$el['screen'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array(@$el['screen'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['screen'];
                }
                @$screen[$key] = $screen[$key] + 1;
                
                
                /**
                 * 
                 * @void os_group
                 */
                $key = '';
                if($el['os_group'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['os_group'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    //$key = '';
                    $key = $el['os_group'];
                }
                @$os_group[$key] = $os_group[$key] + 1;
                
                
                /**
                 * 
                 * @void device_name
                 */
                $key = '';
                if($el['device_name'] == 'Other')
                {
                    $key = 'Máy tính ( Chưa xác định )';
                }else if(in_array($el['device_name'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['device_name'];
                }
                @$device_name[$key] = $device_name[$key] + 1;
                
                
                /**
                 * 
                 * @void browser_name
                 */
                $key = '';
                if($el['browser_name'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['browser_name'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['browser_name'];
                }
                @$browser_name[$key] = $browser_name[$key] + 1;
                
                
                /**
                 * 
                 * @void show_full_time
                 */
                $key = '';
                if($el['show_full_time'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['show_full_time'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['show_full_time'];
                }
                @$show_time[$key] = $show_time[$key] + 1;
                
                
                /**
                 * 
                 * @void os_name
                 */
                $key = '';
                
                if(in_array($el['os_name'], array(null, '', '0')))
                {
                    $dem_os =  $dem_os + 1;
                    $key_os = 'Chưa xác định';
                }else{
                    $dem_os =  $dem_os + 1;
                    $key_os = '';
                    $key_os = $el['os_name'];
                }
                @$array_os[$key_os] = $array_os[$key_os] + 1;
                
                
                /**
                 * 
                 * @void device_name
                 */
                $key = '';
                if($el['device_name'] == 'Other')
                {
                    $dem_device =  $dem_device + 1;
                    $key_device = 'Máy tính';
                }else{
                    $dem_device =  $dem_device + 1;
                    $key_device = 'Di động';
                }
                @$array_device[$key_device] = $array_device[$key_device] + 1;
            }
            
            
            $array['tong_browser'] = $dem;
            $array_device['tong_device'] = $dem_device;
            $array_os['tong_os'] = $dem_os;
            asort($array);
            asort($array_device);
            asort($array_os);
            arsort($browser_name);
            arsort($device_name);
            asort($os_group);
            //asort(@$screen);
            
            //$array['tong_browser'] = $dem;
            //arsort($show_time);
            
        }
        return (object)array('screen' => $screen, 'os_group' => $os_group, 'show_time' => $show_time, 'device_name' => $device_name,'browser' => $array,'browser_ver' => $browser_name,'device' => $array_device, 'os' => $array_os);
    }
    
    public function getLogAccess($number,$offset,$id_user,$id_campaign,$time)
    {
        $limit = $offset ? intval($offset) : '0';
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->limit($number)->offset($offset)->order_by(array('time_vs' => 'DESC'))->get('log_access');
        }
        return array('count' => $count, 'data' => $data);
    }
    public function getCountTime($id_user,$time,$id_campaign,$number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $show_time = array();
        $dem_os = 0;
        $key_os = '';
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->limit($number)->offset($offset)->order_by(array('time_vs' => 'DESC'))->get('log_access');
            
       
            foreach($data AS $le=>$el)
            {
                //$dem =  $dem + 1;
                if($el['show_full_time'] == 'Other')
                {
                    $key = 'Chưa xác định';
                }else if(in_array($el['show_full_time'], array(null, '', '0')))
                {
                    $key = 'Chưa xác định';
                }else{
                    $key = '';
                    $key = $el['show_full_time'];
                }
                @$show_time[$key] = $show_time[$key] + 1;
            }
            //$array['tong_browser'] = $dem;
            //arsort($show_time);
            
            
        }
        return $show_time;
    }
    
    public function getmail($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        //die($check.'--');
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mail');
        }
    }
    
    public function getfacebook($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_facebook');
        }
    }
    
	public function getmobile($id_shop, $id_campaign, $time)
    {
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            return $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mobile');
        }
    }
    
    /**
     * 
     * 
     * bao cao
     * 
     */
     
     
    public function getExport($id_user,$time,$id_campaign)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_access');
        }
        return $data;
    }
	
	public function getExportMobile($id_user,$time,$id_campaign)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_mobile');
        }
        return $data;
    }
    
    
    public function getExportEmail($id_user,$time,$id_campaign)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_mail');
        }
        return $data;
    }
    
    public function getExportFacebook($id_user,$time,$id_campaign)
    {
       
        $id = array();
        
        $mq = $this->db->query('SELECT id, DATE_FORMAT(time_start,"%Y%m%d%H%i%s") AS time1,DATE_FORMAT(time_end,"%Y%m%d%H%i%s") AS time2 FROM campaign WHERE id ='.$id_campaign);
        $check = $mq->num_rows();
        
        if($check > 0)
        {
            if(!in_array($time, array(null, '', '0')))
            {
                $exp = explode('|',$time);
                $time_1 = new MongoInt64($exp[0]);
                $time_2 = new MongoInt64($exp[1]);
            }else{
                $_d = $mq->row_object();
                $time_1 = new MongoInt64($_d->time1);
                $time_2 = new MongoInt64($_d->time2);
            }
            //$count = $this->m_db->where(array('id_campaign' => $id_campaign))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_access');
            $data = $this->m_db->where(array('id_campaign' => intval($id_campaign)))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->order_by(array('time_vs' => 'DESC'))->get('log_facebook');
        }
        return $data;
    }
	
	public function getCampaignuserInfo($id){
		return array('data' => $this->db->query('SELECT * FROM campaign WHERE id = '.$id)->row_object(), 'place' => $this->db->query('SELECT * FROM campaign_place WHERE id_campaign = '.$id)->result_object() ) ;
	}
	
	public function editCampaignUser($address_id,$title,$time_start,$time_end,$note,$id){
		$data = array(
				'title' => $title,
				'time_start' => $time_start,
				'time_end' => $time_end,
				'note' => $note
			);	
			$this->db->update('campaign', $data, "id = ".$id);
			$id_campaign = $id;
			$this->db->delete('campaign_place', array('id_campaign' => $id_campaign)); 
		foreach ( $address_id AS $item ){
			$datax = array( 'id_campaign' => $id_campaign, 'id_place' => $item );
			$this->db->insert('campaign_place', $datax);
		}
		return 200;
	}
    
    
    public function getCountEndUsers($id_user)
    {
        $sql = "SELECT *,DATE_FORMAT(created, '%H:%i:%s %d-%m-%Y') AS time FROM campaign WHERE 1=1 AND id_customers = ".$id_user;
        $sql .= " ORDER BY id DESC";
        
        $xs = $this->db->query($sql)->result_object();
        $id = array();
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $array_os = array();
        $dem_os = 0;
        $key_os = '';
        
        foreach($xs AS $el=>$le)
        {
            $id[] = intval($le->id);
        }
        $data = $this->m_db->where_in('id_campaign', $id)->get('log_access');
        
        foreach($data AS $le=>$el)
        {
            if($el['browser_name_group'] == 'Other')
            {
                $dem =  $dem + 1;
                $key = 'Other';
            }else if(in_array($el['browser_name_group'], array(null, '', '0')))
            {
                $dem =  $dem + 1;
                $key = 'Other';
            }else{
                $dem =  $dem + 1;
                $key = '';
                $key = $el['browser_name_group'];
            }
            @$array[$key] = $array[$key] + 1;
            
            if($el['device_name'] == 'Other')
            {
                $dem_device =  $dem_device + 1;
                $key_device = 'Máy tính';
            }else{
                $dem_device =  $dem_device + 1;
                $key_device = 'Di động';
            }
            @$array_device[$key_device] = $array_device[$key_device] + 1;
            
            
            if(in_array($el['os_group'], array(null, '', '0')))
            {
                $dem_os =  $dem_os + 1;
                $key_os = 'Other';
            }else{
                $dem_os =  $dem_os + 1;
                $key_os = '';
                $key_os = $el['os_group'];
            }
            @$array_os[$key_os] = $array_os[$key_os] + 1;
        }
        $array['tong_browser'] = $dem;
        asort($array);
        $array_device['tong_device'] = $dem_device;
        asort($array_device);
        $array_os['tong_os'] = $dem_os;
        asort($array_os);
        
        return (object)array('browser' => $array,'device' => $array_device, 'os' => $array_os);
    }

    public function getReportExport()
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
            $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
            $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
        }
        return array('data' => $xs);
    }
    
    public function getAjaxReportExport($times,$times_start,$times_end)
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_customers = ".$this->session->userdata('id_user');
        if(!in_array($times_start, array(null,'','0',0))){
            $sql .= " AND (c.time_start BETWEEN '$times_start' AND '$times_end') OR (c.time_end BETWEEN '$times_start' AND '$times_end') OR ('$times_start' AND '$times_end' BETWEEN c.time_start AND c.time_end)";   
        }
        $sql .= " ORDER BY c.id DESC";
        $xs = $this->db->query($sql)->result_object();
        
        if(!in_array($times, array(null, '', '0')))
        {
            $exp = explode('|',$times);
            $time_1 = new MongoInt64($exp[0]);
            $time_2 = new MongoInt64($exp[1]);
            
            
            foreach ( $xs AS $el=>$le ){
                
                $dem = 0;
                $dem_user = 0;
                $tmp = array();
                $tmp['mac'] = '';
                $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
                
                foreach($total AS $ell=>$lle)
                {
                    // Đếm tổng
                    $dem = $dem + 1;
                    if($lle['mac'] != $tmp['mac'])
                    {
                        // số người dùng
                        $tmp['mac'] = $lle['mac'];
                        $dem_user = $dem_user + 1;
                    }
                }
                
                $xs[$el]->total_visit = $dem;
                $xs[$el]->user_visit = $dem_user;
                
                $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_facebook');
                $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mail');
                $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mobile');
                $click = $total_facebook + $total_mail + $total_mobile;
                
                @$outClick = round((($dem - $click) / $dem) * 100);
                @$_click = round(($click / $dem) * 100);
                
                $xs[$el]->personNoClick = $outClick;
                $xs[$el]->personClick = $_click;
                
                /** get tong thơi zan trang dic và so lan hien thi trang dic*/
                $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page FROM campaign WHERE id = '.intval($le->id))->row_object();
                $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
                $xs[$el]->landing_page = $_data_landing_page->landing_page;
                
            }
        }
        
        return array('data' => $xs);
    }
    
    public function ajaxGetCostShop($type){
        $data = $this->db->query('SELECT id, shopName, address, cost FROM shop ORDER BY id ASC')->result_object(); 
        foreach ( $data AS $el=>$le ){
            foreach ( json_decode($le->cost) AS $ee=>$ff ){
                if(  (int)$ee == $type ){
                    $data[$el]->cott = $ff->cost;
                }
            }
        }
        return $data;
    }
    
    public function getHistoryTrade($number,$offset){  
        $limit = $offset ? intval($offset) : '0';
        $sql = "SELECT c.title AS title, lt.money AS money, lt.created AS created FROM campaign AS c JOIN log_trade AS lt ON c.id = lt.id_campaign WHERE c.id_customers = ".$this->session->userdata('id_user');
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
}